package com.example.devinfan.criminalintent.Fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.example.devinfan.criminalintent.Activity.CrimePagerActivity;
import com.example.devinfan.criminalintent.model.Crime;
import com.example.devinfan.criminalintent.model.CrimeLab;
import com.example.devinfan.criminalintent.R;

import java.util.ArrayList;

public class CrimeListFragment extends Fragment {
    //继承listFragment默认自带全屏ListView布局
    //使用自定义布局,可显示空时表和有数据表,重新定义listVIew
    private ArrayList<Crime> mCrimes;
    private CrimeAdapter mCrimeAdapter;
    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setHasOptionsMenu(true);
        mCrimes = CrimeLab.get(getActivity()).getCrimes();
        mCrimeAdapter = new CrimeAdapter(mCrimes);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_list,parent,false);
        final ListView crimesListView = (ListView)view.findViewById(android.R.id.list);
        crimesListView.setAdapter(mCrimeAdapter);

        View emptyView = view.findViewById(android.R.id.empty);
        crimesListView.setEmptyView(emptyView);
        Button addCrimeButton = (Button)emptyView.findViewById(R.id.add_crime_button);

        crimesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Crime c = mCrimeAdapter.getItem(position);
                startCrimePagerActivity(c);
            }
        });

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            //Use floating context menus on
            // Froyo and Gingerbread
            registerForContextMenu(crimesListView);
        }else{
            //Use contextual action bar on Honeycomb and higher
            crimesListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            crimesListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                    //Required,but not used in this implementation
                }

                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.crime_list_item_context,menu);
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                    //Required,but not used in this implementation
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()){
                        case R.id.menu_list_item_delete_crime:
                            CrimeLab crimeLab = CrimeLab.get(getActivity());
                            for (int i = mCrimeAdapter.getCount() - 1; i >= 0; i--){
                                if (crimesListView.isItemChecked(i)){
                                    crimeLab.deleteCrime(mCrimeAdapter.getItem(i));
                                }
                            }
                            mode.finish();
                            mCrimeAdapter.notifyDataSetChanged();
                            return true;
                        default:
                            return false;
                    }
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    //Required,but not used in this implementation
                }
            });

        }

        addCrimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Crime crime = new Crime();
                CrimeLab.get(getActivity()).addCrime(crime);
                startCrimePagerActivity(crime);
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mCrimeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause(){
        super.onPause();
        CrimeLab.get(getActivity()).saveCrimes();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.fragment_crime_list,menu);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
        getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        Crime crime = mCrimeAdapter.getItem(position);
        switch (item.getItemId()){
            case R.id.menu_list_item_delete_crime:
                CrimeLab.get(getActivity()).deleteCrime(crime);
                mCrimeAdapter.notifyDataSetChanged();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.menu_item_new_crime:
                Crime crime = new Crime();
                CrimeLab.get(getActivity()).addCrime(crime);
                startCrimePagerActivity(crime);
                return true;
            /*case R.id.menu_item_show_subtitle:
                if (getActivity().getActionBar() != null) {
                    getActivity().getActionBar().setSubtitle(R.string.subtitle);
                }
                return true;*/
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    //start CrimePagerActivity with this crime
    private void startCrimePagerActivity(Crime crime){
        Intent intent = new Intent(getActivity(),CrimePagerActivity.class);
        intent.putExtra(CrimeFragment.EXTRA_CRIME_ID,crime.getId());
        startActivity(intent);
    }

    private class CrimeAdapter extends ArrayAdapter<Crime>{

        public CrimeAdapter(ArrayList<Crime> crimes){
            super(getActivity(),0,crimes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            //if we weren't given a view ,inflate one
            if(convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime,null);
            }

            Crime c = getItem(position);
            TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
            titleTextView.setText(c.getTitle());
            TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_Item_dateTextView);
            dateTextView.setText(c.getDate().toString());
            CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
            solvedCheckBox.setChecked(c.isSolved());

            return convertView;
        }
    }

}
